#include "../Header/GameObject.h"
#include "../Header/GameComponent.h"


using namespace std;

GameObject :: GameObject(Game * inputGame)
{
	parent = inputGame;

	parent->addGameObject(this);
}

Vec2 GameObject::getPosition()
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	return position;
}

Vec2 GameObject::getDimension()
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	return dimension;
}

Game * GameObject::getParent()
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	return parent;
}

GameComponent * GameObject::getComponent(string compID)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}

	for(vector<GameComponent*>::iterator it = components.begin(); it != components.end(); ++it)
	{
		if((*it)->getID() == compID) return (*it);
	}


	return NULL;
}

void GameObject::addPosition(Vec2 vec)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	position.x = position.x + vec.x;
	position.y = position.y + vec.y;
}

void GameObject::setPosition(Vec2 vec)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	position.x = vec.x;
	position.y = vec.y;
}

bool GameObject::isIntersecting(Vec2 pos, Vec2 dim)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	int l1, l2, r1, r2, t1, t2, b1, b2;
	l1 = position.x;
	r1 = l1 + dimension.y;
	t1 = position.y;
	b1 = t1 + dimension.y;

	l2 = pos.x;
	r2 = l2 + dim.y;
	t2 = pos.y;
	b2 = t2 + dim.y;

	if(t1 < b2) return false;
	if(r2 < l1) return false;
	if(b2 < t1) return false;
	if(r1 < l2) return false;

	return true;
}

void GameObject::setDimension(Vec2 vec)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	dimension.x = vec.x;
	dimension.y = vec.y;
}


void GameObject::addComponent(GameComponent* pComp)
{
	try
	{
		if(this == NULL) throw 1;
	}
	catch(int e)
	{
		cout << "Error: this \"GameObject\" doesn't exist... "<<endl;
	}
	if(pComp->getID() == "Player")
	{
		parent->pointerToPlayer = this;
	}
	pComp->setParent(this);
	components.push_back(pComp);

}

bool GameObject::isSolid()
{
	return solid;
}

void GameObject::setSolid(bool inputSolid)
{
	solid = inputSolid;
}